#include <subControllView.h>

namespace View {

  SubControllView::SubControllView(GooCanvasItem *parent)
    :root(parent),
     currentOnShowItem(Model::SYSTEM_SETTING),
     position(SUB_VIEW_POSITION_LEFT_TOP),
     stillMovingSubItem(false)
  {

  }

  SubControllView::~SubControllView()
  {

  }

  bool SubControllView::AddSubControllItem(Model::AttackType attackType)
  {
    Controller::AllFactory factory;
    SubControllItem *subControllItem = factory.CreateSubControllItem(attackType,root);
    if(NULL != subControllItem)
      subControllItems.insert( std::pair<Model::AttackType,SubControllItem*>(attackType,subControllItem) );
    return true;
  }

  bool SubControllView::DelSubControllItem(Model::AttackType attackType)
  {

    return true;
  }
    
  bool SubControllView::ReceiveBroadcast(Controller::Broadcast *broadcast) 
  {
    //    std::cout<<"SubControllView::ReceiveBroadcast(Controller::Broadcast *broadcast) \n";
    if(Controller::BROADCAST_TYPE_CURRENT_ATTACK == broadcast->GetBroadcastType()){
      if(stillMovingSubItem)
	return false;
      Model::AttackType attackType = static_cast<Model::AttackType>(broadcast->GetDoubleData());
      if( attackType != currentOnShowItem ){
	ViewConf *conf = ViewConf::GetViewConf();
	int X,Y;      
	switch(position)
	  {
	  case SUB_VIEW_POSITION_LEFT_TOP:{
	    X = -1;
	    Y = -1;
	  }break;
	  case SUB_VIEW_POSITION_LEFT_MID:{
	    X = 0;
	    Y = -1;
	  }break;
	  case SUB_VIEW_POSITION_LEFT_BOTTOM:{
	    X = 1;
	    Y = -1;
	  }break;
	  case SUB_VIEW_POSITION_MID_TOP:{
	    X = -1;
	    Y = 0;
	  }break;
	  case SUB_VIEW_POSITION_MID_BOTTOM:{
	    X = 1;
	  Y = 0;
	  }break;
	  case SUB_VIEW_POSITION_RIGHT_TOP:{
	    X = -1;
	    Y = 1;
	  }break;
	  case SUB_VIEW_POSITION_RIGHT_MID:{
	    X = 0;
	    Y = 1;
	  }break;
	  default:{
	    X = 1;
	    Y = 1;
	  }
	  }
	stillMovingSubItem = true;
	subControllItems[currentOnShowItem]->RunTo(X*(conf->GetDoubleData("mainWindowLength")),
						   Y*(conf->GetDoubleData("mainWindowHeigth")));
	subControllItems[attackType]->RunTo(0,0);
	currentOnShowItem = attackType;
	if( SUB_VIEW_POSITION_RIGHT_BOTTOM == position )
	  position = SUB_VIEW_POSITION_LEFT_TOP;
	else{
	  int i = static_cast<int>(position);
	  i ++;
	  position = static_cast<SubControllViewPosition>(i);
	}
      }
    }else if(Controller::BROADCAST_TYPE_SUBITEM_ANIMATE_STOP == broadcast->GetBroadcastType()){
      stillMovingSubItem = false;
    }else{
      for(std::map<Model::AttackType,SubControllItem*>::iterator it = subControllItems.begin();  
	  it != subControllItems.end(); it ++)
	it->second->OnReceive(broadcast);
    }
    
    return true;
  }



}
